﻿using Coolvis.UI.Theme.Console;
using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Metadata;
using CPPEI.Coolzon.UI.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Theme.Fashion
{
    /// <summary>
    /// TranscribeWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TranscribeWindow : Window
    {
        private List<UI.Views.InterceptActionEvent> listInterceptActionEvent;
        private Scene _Scene;
        public TranscribeWindow()
        {
            listInterceptActionEvent = new List<InterceptActionEvent>();
            InitializeComponent();
            EventBus.Instance.Register(this);

        }
        public TranscribeWindow(Scene scene)
        {
            listInterceptActionEvent = new List<InterceptActionEvent>();
            InitializeComponent();
            EventBus.Instance.Register(this);
            this._Scene = scene;
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (listInterceptActionEvent != null && listInterceptActionEvent.Count > 0)
            {
                CustomAction action = new CustomAction();
                action.Steps = new List<CustomActionStep>();
                foreach (InterceptActionEvent actionEvent in listInterceptActionEvent)
                {
                    action.Steps.Add(new CustomActionStep { ScriptCode = actionEvent.ScriptCode });
                }
                List<CustomAction> customActions = new List<CustomAction>();
                customActions.Add(action);

                string actionPath = CZEnvironment.GetCustomActionFilePath(UI.UIContext.Current.UserIdentity, UI.UIContext.Current.SolutionName, _Scene.Name);

                CPPEI.Coolzon.Metadata.ParserV1.SolutionXmlParser xmlParse = new Metadata.ParserV1.SolutionXmlParser();
                xmlParse.SaveCustomActionList(actionPath, customActions);
                this.Close();
            }

        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        [Subscribe]
        public void OnInterceptActionEvent(UI.Views.InterceptActionEvent @event)
        {
            if (@event != null)
            {
                listInterceptActionEvent.Add(@event);
            }
            this.ltbTrabscribe.ItemsSource = null;
            this.ltbTrabscribe.ItemsSource = listInterceptActionEvent;
        }

        protected override void OnClosed(EventArgs e)
        {
            EventBus.Instance.Unregister(this);
            base.OnClosed(e);
        }
    }
}
